//
//  XVToolBar.swift
//  Hunter
//
//  Created by oher on 15/6/24.
//  Copyright © 2015年 刘🐑. All rights reserved.
//

import UIKit

class XVToolBar: UIView {

    @IBOutlet weak var btn1: UIButton!
    @IBOutlet weak var btn2: UIButton!
    
    
    
    var line : UIView?
    var selectedButton : UIButton?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        let line = UIView()
        line.backgroundColor = UIColor.redColor()
        addSubview(line)
        self.line = line
        
        
    }
    
    @IBAction func didClickButton(sender: UIButton) {
        
        if sender.isEqual(selectedButton) {
            return
        }else{
            selectedButton = sender
        }
        
        
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            // line移动
            self.moveLinePosition(sender)
            
            // scrollView跟随滚动,代理方法
        })
        
        
    }
    
    // line移动
    func moveLinePosition(sender: UIButton){
        self.line?.center = CGPointMake(sender.center.x, CGRectGetMaxY(sender.frame))
        self.line?.bounds = CGRectMake(0, 0, 50, 2)
        
    }
    
//    let scrollView : UIScrollView?
//    
//    //scrollView跟随滚动
//    func moveScrollViewPosition(){
//        
//        let buttonTag = selectedButton?.tag
//        let w = UIScreen.mainScreen().bounds.width
//        let x = CGFloat(buttonTag!) * w
//        let y = CGRectGetMaxY(self.frame)
//        let h = UIScreen.mainScreen().bounds.height - y - 44
//        
//        scrollView?.frame = CGRectMake(x, y, w, h)
//        
//        scrollView?.contentOffset = CGPointMake(w * CGFloat(buttonTag!), CGRectGetMaxY(self.frame))
//        
//    }
    
    
    override func layoutSubviews() {
        moveLinePosition(btn1)
        selectedButton = btn1
        
    }


    

    func didSelectBtnAtIndex(index : Int){
        
//        let btn = subviews.
//        print(subviews.count)
//        UIView.animateWithDuration(1.0, animations: { () -> Void in
//            self.line?.center = CGPointMake(btn.center.x, CGRectGetMaxY(btn.frame))
//            //self.line?.bounds = CGRectMake(0, 0, 45, 2)
//
//        })
    }
    
    
    
}
